/**
 * @description UserModel Service
 */

 const { UserModel } = require('../db/index')
 const Op = require('sequelize').Op
 
 /**
  * 新用户注册
  * @param {string} userid 用户id
  * @param {string} nikename 昵称
  * @param {string} city 城市
  * @param {number} isVip 是否VIP
  * @param {string} createtime 注册时间
  */
 async function createUser(params) {
   const result = await UserModel.create(params)
   const data = result.dataValues
   return data
 }
 
 /**
  * 
  * @param {string} username 用户名 
  */
 async function getUserInfo(params) {
   // 查询条件
   const whereOption = {
    phone: params.phone
   }
   const result = await UserModel.findOne({
     where: whereOption
   })
   if (result === null) {
     return result
   }
   return result.dataValues
 }
 
 /**
  * 修改用户信息
  * @param {*} param0 要修改的用户信息
  * @param {*} param1 查询条件
  */
  async function updateUser(
    params,
    id
  ) {
    // 拼接修改内容
    const updateObj = params
  
    // 拼接查询条件
    const whereOption = {}
    if (id) {
      whereOption.id = id
    }
  
    // 执行修改
    const result = await UserModel.update(updateObj, {
      where: whereOption
    })
    return result[0] > 0
  }
 
 /**
  * 删除用户
  * @param {string} userid 用户id 
  */
 async function deleteUser(userid) {
   const result = await UserModel.destroy({
     where: {
       userid
     }
   })
   return result > 0
 }
 
 /**
  * 分页查询所有用户
  * @param {number} page 当前页码
  * @param {number} pageSize 每页条数
  */
 async function queryAll({ nikename, userid, page = 1, pageSize = 10, key }) {
   const whereOption = {}
   if (nikename) {
     whereOption.nikename = {
       [Op.like]: `%${nikename}%`
     }
   }
 
   if (page < 1) {
     page = 1
   } 
   const result = await UserModel.findAll({
     limit: pageSize,
     offset: (page - 1) * pageSize,
     order: [
       ['createtime', 'desc']
     ],
     where: whereOption
   })
 
   return {
     page,
     pageSize,
     count: result.count,
     data: result.map(user => user.dataValues)
   }
 }
 async function deleteData(id) {
   const result = await UserModel.destroy({
     where: {
       id
     }
   })
   return result > 0
 }
 
async function querySome(params) {
  try {
    const {
      page = 1,
      pageSize = 10, 
      id,
      role,
      key
    } = params
    const whereOption = {}
    if (id) {
      whereOption.id = id
    }
    if (role) {
      whereOption.role = role;
    }
    if (key) {
      whereOption[Op.or] = [
        { phone: { [Op.like]: `%${key}%` } },  
        { nickname: { [Op.like]: `%${key}%` } },
        { role: { [Op.like]: `%${key}%` } },
      ];
    }
    if (page < 1) {
      page = 1
    }
    console.log(whereOption)
    const result = await UserModel.findAll({
      limit: pageSize,
      offset: (page - 1) * pageSize,
      where: whereOption
    })
    const total = await UserModel.findAll()
    return {
      page,
      pageSize,
      data: result || [],
      total: total.length
    }
  } catch (error) {
    console.log(error)
  }

  
}
async function checkUser(params) {
  const whereOption = {
    phone: params.phone,
    password: params.password
  };
  const result = await UserModel.findAll({
    where: whereOption,
  });
  return {
    data: result.length
  };
}
 module.exports = {
   createUser,
   getUserInfo,
   updateUser,
   deleteUser,
   queryAll,
   deleteData,
   querySome,
   checkUser
 }
 